class AValidatoryBanana
  
  def self.validate type, value, constraint
    begin
      self.send "validate_#{type.downcase}".to_sym, value, constraint
    rescue
      # if in doubt, treat it as text
      validate_text value, nil
    end
  end
  
  private
  
  def self.validate_string value, constraint
    # strings do not contain carriage returns
    # any constraint will be a regex
    return false if value[/[\r\n]/]
    if constraint
      r = Regexp.new constraint
      m = r.match value
      return false unless m
      return false unless m.count == 1
      return false unless m[0] == value
    end
    true
  end
  
  def self.validate_text value, constraint
    # anything goes in a text field unless a constraint has been provided
    if constraint
      r = Regexp.new constraint
      m = r.match value
      return false unless m
      return false unless m.count == 1
      return false unless m[0] == value
    end
    true
  end
  
end